//
//  NKRunInfoView.m
//  NKMapTest
//
//  Created by SongGang on 2017/3/16.
//  Copyright © 2017年 NK. All rights reserved.
//

#import "NKRunInfoView.h"


#define controlHeight 20

@interface NKRunInfoView()

@property (nonatomic, assign) BOOL isOpen;

@property (nonatomic, assign) CGRect originFrame;

@end

@implementation NKRunInfoView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.isOpen = YES;
        self.originFrame = self.frame;
        
        self.backgroundColor = [NKBlueColor colorWithAlphaComponent:0.5];
        
        self.control = [UIButton buttonWithType:UIButtonTypeCustom];
        self.control.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), controlHeight);
        self.control.backgroundColor = NKBlueColor;
        [self.control setTitle:@"" forState:UIControlStateNormal];
        self.control.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.control addTarget:self action:@selector(actionSwitch) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.control];
        
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, controlHeight, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)-controlHeight)];
        self.textView.backgroundColor = [UIColor clearColor];
        self.textView.textColor = [UIColor whiteColor];
        self.textView.font = [UIFont systemFontOfSize:12];
        self.textView.editable = NO;
        self.textView.selectable = NO;
        
        [self addSubview:self.textView];
        
    }
    return self;
}

- (void)actionSwitch
{
    _isOpen = ! _isOpen;
    
    if (_isOpen)
    {
        [UIView animateWithDuration:0.25 animations:^{
            self.frame = _originFrame;
            self.textView.frame = CGRectMake(0, controlHeight, self.frame.size.width, self.frame.size.height-controlHeight);
        }];
    }
    else
    {

        [UIView animateWithDuration:0.25 animations:^{
            self.frame = CGRectMake(self.originFrame.origin.x, self.originFrame.origin.y, self.originFrame.size.width, controlHeight);
            self.textView.frame = CGRectMake(0, 0, 0, 0);
        }];
    }
}

- (void)showStatusWith:(AMapRouteRecord *)routeRecord
{
    CLLocation *location = [routeRecord endLocation];
    
    NSMutableString *info = [[NSMutableString alloc] init];
    [info appendString:@"经纬度: "];
    [info appendString:[NSString stringWithFormat:@"%.3f, %.3f\n", location.coordinate.latitude,location.coordinate.longitude]];
    
    [info appendString:@"速度: "];
    double speed = location.speed > 0 ? location.speed : 0;
    [info appendString:[NSString stringWithFormat:@"<%.1fm/s(%.1fkm/h)>\n", speed, speed * 3.6]];
    
    [info appendString:@"精度: "];
    [info appendString:[NSString stringWithFormat:@"%.2fm\n", location.horizontalAccuracy]];
    
    [info appendString:@"海拔: "];
    [info appendString:[NSString stringWithFormat:@"%.2fm\n", location.altitude]];
    
    [info appendString:@"实际距离: "];
    [info appendString:[NSString stringWithFormat:@"%.1fm\n", [self getDistance:routeRecord.locations]]];
    
    [info appendString:@"方向: "];
    [info appendString:[NSString stringWithFormat:@"%.f度\n", self.angle]];
    
    [info appendString:@"气压: "];
    [[NKHealthKitManager shareInstance] getPressure:^(double value, NSError *error) {
         [info appendString:[NSString stringWithFormat:@"%.2fkPa\n", value]];
        _textView.text = info;
        
    }];

}

- (double)getDistance:(NSArray *)locationArray {

    double distance = 0.0;
    
    if (locationArray.count > 1) {
        
        for (int i = 0; i < locationArray.count - 1; i++) {
            
            CLLocation *oldLocation = locationArray[i];
            CLLocation *newLocation = locationArray[i+1];
           distance += [newLocation distanceFromLocation:oldLocation];
        }
    }
    
    return distance;
}

@end
